<?php

/*
 * transfert/planning-transfert.phtml
 */
?>
<?php

use RHX\Helper\RHDateTime;
?>
<?php
$lundi		 = new \DateTime($this->lundi);
$dimanche	 = new \DateTime($this->dimanche);
$dimanche->modify('next day');
$interval	 = new \DateInterval('P1D');
$dateList	 = new \DatePeriod($lundi, $interval, $dimanche);
$dates		 = array();
foreach ($dateList as $date) {
//    var_dump($date);
    $rhDate			 = new RHDateTime($date->format('Y-m-d'));
    $dates[$rhDate->date()]	 = $rhDate->dateFr();
}
//var_dump($lundi);
//var_dump($dimanche);
//var_dump($interval);
//var_dump($dateList);
//var_dump($dates);
?>

<!--h3>Transfert pour le site <?php echo $site->libelle; ?></h3-->

<!-- ETAPE 1 -->
<div id="step1">
    <div style='color: #fff; border: solid 2px #168900; background: #116c00; margin-bottom: 1em; padding: 5px;'>
	<h4>Sélectionner le site de l'employé à transferer</h4>
    </div>
    <div class='site-container'>
<?php foreach ($sites as $s): ?>
    	<div class='site-one'>
    	    <a href='#' data-id='<?php echo $s->id; ?>' class='site-link'><?php echo $s->libelle; ?></a>
    	</div>
<?php endforeach; ?>
    </div>
    <div style='width: 99%; height: 2.5em; margin: 10px auto; box-shadow: 0px 0px 5px 3px #2c8bfd;'>
	<span>&nbsp;</span>
	<button class='btn btn-info step2' style='float: right; margin: 5px;'>Suivant</button>
    </div>
</div>

<!-- ETAPE 2 -->
<div id='step2' style='display: none;'>
    <div style='color: #fff; border: solid 2px #168900; background: #116c00; margin-bottom: 1em; padding: 5px;'>
	<h4>Sélectionner l'employé à Transferer vers <b><?php echo $site->libelle; ?></b></h4>
    </div>
    <div class='site-employes'>
	<!-- AJAX -->
	<div class='rh-error'>
	    Sélectionner un Site
	</div>
    </div>
    <div style='width: 99%; height: 2.5em; margin: 10px auto; box-shadow: 0px 0px 5px 3px #2c8bfd;'>
	<span>&nbsp;</span>
	<button class='btn btn-info step3' style='float: right; margin: 5px;'>Suivant</button>
	<button class='btn btn-inverse step1' style='float: right; margin: 5px;'>Précédent</button>
    </div>
</div>

<!-- ETAPE 3 -->
<div id='step3' style='display: none'>
    <div style='color: #fff; border: solid 2px #168900; background: #116c00; margin-bottom: 1em; padding: 5px;'>
	<h4>Remplir les Informations pour le transfert de <span id='employe-name' style='font-weight: bolder; text-decoration: underline'></span> vers le site <u><?php echo $site->libelle; ?></u></h4>
    </div>
    <label>
	<span>Date de Transfert</span>
	<select id='date'>
	    <?php foreach ($dates as $date => $dateFr): ?>
    	    <option value='<?php echo $date; ?>'><?php echo $dateFr; ?></option>
	    <?php endforeach; ?>
	</select>
    </label>
    <div class='newLabel'>
	<span>Heure d'Entrée</span>
	<select id='entree_heure' class='heure_minute'>
	    <?php for ($i = 0; $i < 24; $i++): ?>
		<?php $value = ($i < 10) ? '0' . $i : $i; ?>
    	    <option value='<?php echo $value; ?>'><?php echo $value; ?></option>
	    <?php endfor; ?>
	</select>&nbsp;:&nbsp;
	<select id='entree_minute' class='heure_minute'>
	    <?php for ($i = 0; $i < 60; $i+= 5): ?>
		<?php $value = ($i < 10) ? '0' . $i : $i; ?>
    	    <option value='<?php echo $value; ?>'><?php echo $value; ?></option>
	    <?php endfor; ?>
	</select>
    </div>
    <div class='newLabel'>
	<span>Heure de Sortie</span>
	<select id='sortie_heure' class='heure_minute'>
	    <?php for ($i = 0; $i < 24; $i++): ?>
		<?php $value = ($i < 10) ? '0' . $i : $i; ?>
    	    <option value='<?php echo $value; ?>'><?php echo $value; ?></option>
	    <?php endfor; ?>
	</select>&nbsp;:&nbsp;
	<select id='sortie_minute' class='heure_minute'>
	    <?php for ($i = 0; $i < 60; $i+= 5): ?>
		<?php $value = ($i < 10) ? '0' . $i : $i; ?>
    	    <option value='<?php echo $value; ?>'><?php echo $value; ?></option>
	    <?php endfor; ?>
	</select>
    </div>
    <label>
	<span>Pause</span>
	<input type='checkbox' id='pause'>
    </label>
    <label>
	<span>Repas</span>
	<input type='checkbox' id='repas'>
    </label>
    <label>
	<span>Commmentaire</span>
	<a href='#' id='commentaire' data-type='textarea'></a>
    </label>
    <div style='width: 99%; height: 2.5em; margin: 10px auto; box-shadow: 0px 0px 5px 3px #2c8bfd;'>
	<span>&nbsp;</span>
	<button class='btn btn-success done' style='float: right; margin: 5px;'>Effectuer le Transfert</button>
	<button class='btn btn-inverse step2' style='float: right; margin: 5px;'>Précédent</button>
    </div>
</div>


<!-- Formulaire -->
<form id='transfertForm' action='#' method='post'>
    <?php $dateAff = ''; ?>
    <?php
    foreach ($dates as $date => $dateFr) {
	$dateAff = $date;
	break;
    }
    ?>
    <input type='hidden' id='data-matricule' name='matricule' value=''>
    <input type='hidden' id='data-date' name='jour' value='<?php echo $dateAff; ?>'>
    <input type='hidden' id='data-entree' name='entree' value='00:00'>
    <input type='hidden' id='data-sortie' name='sortie' value='00:00'>
    <input type='hidden' id='data-pause' name='pause' value='0'>
    <input type='hidden' id='data-repas' name='repas' value='0'>
    <input type='hidden' id='data-commentaire' name='commentaire' value=''>
    <input type='hidden' id='data-depart' name='depart' value='0'>
    <input type='hidden' id='data-arrivee' name='arrivee' value='<?php echo $site->id; ?>'>
</form>

<!-- SCRIPTS -->
<script type='text/javascript'>
    // Démarrage
    $(function(){
	// Etape 1
	$('.step1').click(function(e){
	    e.preventDefault();
	    $('#step2').hide();
	    $('#step3').hide();
	    $('#step1').show('normal');
	});
	// Etape 2
	$('.step2').click(function(e){
	    e.preventDefault();
	    $('#step3').hide();
	    $('#step1').hide();
	    $('#step2').show('normal');
	});
	// Etape 3
	$('.step3').click(function(e){
	    e.preventDefault();
	    $('#step1').hide();
	    $('#step2').hide();
	    $('#step3').show('normal');
	});
	// Done
	$('.done').click(function(e){
	    e.preventDefault();
	    matricule = $('#data-matricule').val();
	    date = $('#data-date').val();
	    entree = $('#data-entree').val();
	    sortie = $('#data-sortie').val();
	    pause = $('#data-pause').val();
	    repas = $('#data-repas').val();
	    allow = $.ajax({
		type: 'post',
		url : '<?php echo $this->url('RHX/default', array('controller' => 'transfert', 'action' => 'allowTransfert')) ?>',
		data: '&matricule=' + matricule + '&date=' + date + '&entree=' + entree + '&sortie=' + sortie
	    });
	    allow.done(function(result){
		if(!result.allowed){
		    showError("Attention! Le transfert de l'employé ne peut aboutir<br>Il existe déjà un transfert aux horaires indiquées<br>" + result.message);
		    return false;
		}
		ajax = $.ajax({
		    type: 'post',
		    url : '<?php echo $this->url('RHX/default', array('controller'	 => 'transfert', 'action'	 => 'makeTransfert')) ?>',
		    data: $('#transfertForm').serialize()
		});
		ajax.done(function(data){
		    //alert('done');
		    $('#' + ID).click();
		    $('#dialog1').dialog('close');
		});
	    });
	});
	// Site
	$('.site-link').click(function(e){
	    e.preventDefault();
	    id = $(this).attr('data-id');
	    ajax = $.ajax({
		type: 'post',
		url : '<?php echo $this->url('RHX/default', array('controller'	 => 'transfert', 'action'	 => 'getEmployeListBySite')) ?>',
		data: '&site=' + id
	    });
	    ajax.done(function(data){
		$('#step2 .site-employes').html(data);
		$('.step2').click();
		$('#data-depart').val(id);
	    });
	});
	// Editable
//	$('#entree').editable({
//	    title: "Heure d'Entrée",
//	    validate: function(value){
//		$('#data-entree').val(value.hour() + ':' + value.minute());
//	    }
//	});
//	$('#sortie').editable({
//	    title: "Heure de Sortie",
//	    validate: function(value){
//		$('#data-sortie').val(value.hour() + ':' + value.minute());
//	    }
//	});
	$('#commentaire').editable({
	    title: "Edition du Commentaire",
	    validate: function(value){
		$('#data-commentaire').val(value);
	    }
	});
	$('#date').change(function(){
	    val = $('#date option:selected').val();
	    $('#data-date').val(val);
	});
	$('#pause').click(function(){
	    if($(this).prop('checked')){
		$('#data-pause').val(1);
	    }else{
		$('#data-pause').val(0);
	    }
	});
	$('#repas').click(function(){
	    if($(this).prop('checked')){
		$('#data-repas').val(1);
	    }else{
		$('#data-repas').val(0);
	    }
	});
	// Temps
	$('#entree_heure, #entree_minute, #sortie_heure, #sortie_minute').change(function(){
	    verifyTime();
	});
    });

    function verifyTime(){
	entreeHeure = parseInt($('#entree_heure option:selected').val());
	entreeMinute = parseInt($('#entree_minute option:selected').val());
	sortieHeure = parseInt($('#sortie_heure option:selected').val());
	sortieMinute = parseInt($('#sortie_minute option:selected').val());

	diffHeure = 0;
	diffMinute = 0;

	if(sortieMinute < entreeMinute){
	    sortieHeure--;
	    sortieMinute+= 60;
	}
	diffMinute = sortieMinute - entreeMinute;
	diffHeure = sortieHeure - entreeHeure;

	// Durée de la pause
	if(diffHeure >= 5){
	    $('#pause').prop('checked', 'checked');
	    $('#data-pause').val(1);
	}else{
	    $('#pause').prop('checked', '');
	    $('#data-pause').val(0);
	}

	// Entre 12h et 14h
	if((entreeHeure >= 12 && entreeHeure <= 14) || (sortieHeure >= 12 &&sortieHeure <= 14)){
	    /*$('#pause').prop('checked', 'checked');
	    $('#data-pause').val(1);*/
	    $('#repas').prop('checked', 'checked');
	    $('#data-repas').val(1);
	}
	//validité de l'heure de sortie
	if(sortieMinute >= 60){
	    sortieHeure++;
	    sortieMinute-= 60;
	}

	// Assignation dans le formulaire
	affHeure = (entreeHeure < 10) ? '0' + entreeHeure : entreeHeure;
	affMinute= (entreeMinute < 10) ? '0' + entreeMinute : entreeMinute;
	$('#data-entree').val((affHeure + ':' + affMinute));
	affHeure = (sortieHeure < 10) ? '0' + sortieHeure : sortieHeure;
	affMinute= (sortieMinute < 10) ? '0' + sortieMinute : sortieMinute;
	$('#data-sortie').val((affHeure + ':' + affMinute));
    }
</script>

<style type='text/css'>
    #step3 label, .newLabel{
	display: block;
	width: 450px;
    }
    #step3 label span, .newLabel span{
	display: inline-block;
	width: 45%;
	text-decoration: underline;
    }
    #step3 label a, .newLabel a{
	display: inline-block;
	max-width: 300px;
    }
    .heure_minute{
	width: 50px;
    }
</style>